﻿/* 
 * author: www.cnblogs.com/luminji
 * date  : 2011/12/09
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Luminji.Exam.Infrastructure.Repositories.Users;
using Luminji.ExamWeb.Helpers;
using Luminji.ExamWeb.Models;
using Luminji.Exam.Model.Users;
using System.Web.UI;
using Luminji.Core.Web.Cookie;
using Luminji.Core.Web;
using Luminji.Infrastructure.UnitOfWork;

namespace Luminji.ExamWeb.Controllers
{
    public class AccountController : BaseExamControl
    {
        ExamUserService _service;

        public AccountController()
        {
            IUnitOfWork uw = new UnitOfWork();
            _service = new ExamUserService(new ExamUserRepository(uw));
        }

        public ActionResult LogOn()
        {
            return View();
        }

        public ActionResult Index()
        {
            LogOnModel currentUser = CookieHelper.GetCookieValue<LogOnModel>(Request, "LogOnModel");
            if (currentUser == null)
            {
                return RedirectToAction("LogOn", "Account");
            }
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            var loginState = _service.CheckUser(model.UserName, model.Password);
            if (loginState == LoginState.UserNotExsisted)
            {
                ViewBag.LoginError = "对不起，当前用户不存在！";
            }
            else if (loginState == LoginState.PasswordWrong)
            {
                ViewBag.LoginError = "对不起，密码错误，请重新输入！";
            }
            else
            {
                return SetCookieAndRedirectToIndex(model);
            }
            return View(model);
        }

        private ActionResult SetCookieAndRedirectToIndex(LogOnModel model)
        {
            _service.SetLoginStates(model.UserName);
            CookieHelper.SetCookieValue<LogOnModel>(Response, "LogOnModel", model);
            return RedirectToAction("Index", "Account");
        }

        public ActionResult LogOff()
        {
            CookieHelper.DeleteCookieValue(Response, "LogOnModel");
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                ExamUser user = new ExamUser()
                {
                    CurrentUserName = model.UserName,
                    UserKey = model.Password
                };
                _service.AddUser(user);
            }
            return SetCookieAndRedirectToIndex(
                new LogOnModel()
                    {
                        UserName = model.UserName,
                        Password = model.Password,
                        RememberMe = true
                    });
        }

        [Authorize]
        public ActionResult ChangePassword()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                // ChangePassword will throw an exception rather
                // than return false in certain failure scenarios.
                bool changePasswordSucceeded;
                try
                {
                    MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
                    changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
                }
                catch (Exception)
                {
                    changePasswordSucceeded = false;
                }

                if (changePasswordSucceeded)
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

    }
}
